package com.luoying.itew.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.luoying.itew.R;
import com.luoying.itew.service.MyIntentService;
import com.luoying.itew.util.FileUtils;
import com.luoying.itew.util.LogUtils;
import com.luoying.itew.util.ZXWUtils;

import java.io.File;



/**
 * Created by asus on 2017/9/11.
 */

public class UpdataDialog {
    public static final String TAG = "UpdataDialog";
    public Context mContext;

    private AlertDialog alertDialog = null;
    private ProgressBar progressBar1;  //进度
    private TextView textView1;     //百分比进度提示
    private String url = null;          //下载地址

    public UpdataDialog(Context mContext) {

        this.mContext = mContext;
    }

    //提示dialog
    public void setDialog() {
        // 构造软件下载对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(R.string.soft_updating);
        // 给下载对话框增加进度条
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.progress, null);
        progressBar1 = (ProgressBar) v.findViewById(R.id.progressBar1);
        textView1 = (TextView) v.findViewById(R.id.textView1);
        builder.setView(v);
        builder.setCancelable(false);
        alertDialog = builder.create();
        alertDialog.show();
    }

    private void setProgressBar1(int progress) {

        if (progressBar1 != null) {
            progressBar1.setProgress(progress);
        }
    }

    /**
     * 显示软件更新对话框
     */
    public void showNoticeDialog(String url) {
        this.url = url;
        setDialog();
        staticeService();
    }

    private void staticeService() {
        // 启动Service 开始下载
        MyIntentService.startUpdate(mContext,url, FileUtils.upgradeFilePath(), new MyIntentService.OnProgressListener() {
            @Override
            public void onProgress(long progress) {
                //更新对话框进度条
                progressBar1.setProgress((int) progress);
                textView1.setText(progress+"%");
                LogUtils.d(TAG,"onProgress == " + progress);
            }

            @Override
            public void onSuccess(boolean isSuccess) {
                alertDialog.dismiss();
                alertDialog = null;
                //失败提示
                LogUtils.d(TAG,"isSuccess == " + isSuccess);
                if (!isSuccess) {
                    Toast.makeText(mContext,"更新失败！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //获取保存路径
    private String Route() {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
            Toast.makeText(mContext,"SD卡不可用",Toast.LENGTH_SHORT).show();
            return null;
        }
        File file = new File("/sdcard/Antai/");
        file.mkdirs();// 创建文件夹
        LogUtils.d(TAG, ZXWUtils.packageName(mContext));
        String fileNames = "/sdcard/Antai/antai.apk";
        return fileNames;
    }

}
